<template>
  <div class="userAlter">
    <div class="box" @click="$event.stopPropagation()">
      <div class="title">
        <h2>{{ title }}</h2>
        <el-icon @click="close"><Close /></el-icon>
      </div>
      <div class="input">
        <el-form
          :model="ruleForm"
          class="demo-ruleForm"
          :rules="rules"
          ref="ruleFormRef"
          label-width="auto"
        >
          <el-form-item label="用户名">
            <el-input v-model="ruleForm.name" size="large" disabled />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input v-model="ruleForm.phone" size="large" />
          </el-form-item>
          <el-form-item label="邮箱" prop="email">
            <el-input v-model="ruleForm.email" size="large" />
          </el-form-item>
        </el-form>
      </div>
      <div class="button">
        <el-button @click="close">取消</el-button>
        <el-button type="primary" @click="ensure">确定</el-button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { Close } from '@element-plus/icons-vue'
import { useCounterStoreB } from '@/stores/temporary'
import { getUserAlter } from '@/api/userAlter'
const title = ref('修改用户')
const StoreB = useCounterStoreB()
const showAddUsers = ref(StoreB.addState)
interface rowObj {
  name: string
  phone: number
  email: string
}
const ruleForm = ref({
  name: '',
  phone: 0,
  email: '',
}) as Ref<rowObj>
const rules = reactive({
  name: [
    { required: true, message: '请输入用户名', trigger: 'blur' },
    { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' },
  ],
  phone: [
    { required: true, message: '请输入手机号', trigger: 'blur' },
    { min: 3, max: 11, message: '长度在 3 到 11个字符', trigger: 'blur' },
  ],
  email: [
    { required: true, message: '请输入邮箱', trigger: 'blur' },
    { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' },
  ],
})

const close = () => {
  showAddUsers.value = !showAddUsers.value
  StoreB.stateFun(showAddUsers.value)
  ruleForm.value.name = ''
  ruleForm.value.phone = 0
  ruleForm.value.email = ''
}
const Uid = ref(StoreB.rowId)
const ensure = async () => {
  try {
    const res = await getUserAlter({
      id: Uid.value,
      email: ruleForm.value.email,
      mobile: ruleForm.value.phone,
    })
    console.log(res)
    close()
  } catch (err) {
    console.log(err)
  }
}
watch(
  () => StoreB.addState,
  (newValue) => {
    showAddUsers.value = newValue
  },
)

watch(
  () => StoreB.rowObject,
  (newValue) => {
    ruleForm.value.name = newValue.name
    ruleForm.value.phone = newValue.phone
    ruleForm.value.email = newValue.email
  },
  { deep: true },
)
watch(
  () => StoreB.rowId,
  (newValue) => {
    Uid.value = newValue
  },
)
watch(
  () => StoreB.titleState,
  (newValue) => {
    title.value = newValue
  },
)
</script>

<style scoped>
.box {
  width: 50vw;
  background: #fff;
  padding: 15px;
  display: flex;
  flex-wrap: wrap;
  align-content: space-between;
  .title {
    width: 100%;
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 5%;
    h2 {
      margin: 0px;
      color: #343739;
    }
  }
  .input {
    width: 100%;
    display: flex;
    justify-content: center;
    flex-direction: column;
  }
  .button {
    width: 100%;
    display: flex;
    justify-content: flex-end;
    align-items: center;
  }
}
</style>
